//
//  WBHomeViewController.swift
//  传智微博
//
//  Created by Pro on 2017/8/1.
//  Copyright © 2017年 liuyanwei. All rights reserved.
//

import UIKit

//定义全局常量，尽量使用fileprivate修饰，否则到处可以访问
fileprivate let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }

    //列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    //加载数据
    override func loadData() {
        
        listViewModel.loadStatus(pullup: isPullup) { (isSuccess, shouldRefresh) in
            print("加载数据结束")
            print(self.listViewModel.statusList.last?.text ?? "")
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标志
            self.isPullup = false
            if shouldRefresh {
                //刷新表格
                self.tableView?.reloadData()
            }
        }
      
        
        
        //模拟延时加载数据-》dispatch_after
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
//            for i in 0..<15 {
//                if self.isPullup {
//                    self.statusList.append("上拉\(i)")
//                }else {
//                    self.statusList.insert(i.description, at: 0)
//                }
//                
//            }
//           
//        }
        
    }
    
    //显示好友
    @objc fileprivate func showFriends() {
        print(#function)
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}

//MARK ： - 表格数据源方法
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
    }
}

//设置界面
extension WBHomeViewController {
    //重写父类方法
    override  func setupTableView () {
        super.setupTableView()
//        navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", style:.plain, target: self, action: #selector(showFriends))
        
        navItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", target: self, action: #selector(showFriends))
    }
}
